﻿using AVCUMPlatform.CORPWXService.Interface;
using AVCUMPlatform.DAL.Implement;
using AVCUMPlatform.DingTalkService.Interface;
using AVCUMPlatform.DingTalkService.Service;
using AVCUMPlatform.Entities;
using AVCUMPlatform.Entities.Enum;
using AVCUMPlatform.HelperLibrary;
using AVCUMPlatform.MiniWXService;
using AVCUMPlatform.SMSService.Interface;
using AVCUMPlatform.WXService.Interface;
using AVCUMPlatform.WXService.Service;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace AVCUMPlatform.BLL.Implement
{
    /// <summary>
    /// 平台管理类
    /// </summary>
    public class ApiHelperFactory
    {
        /// <summary>
        /// 微信Api 帮助类
        /// </summary>
        private static IWXPlatformApiHelper _wxInstance { get; set; }
        /// <summary>
        /// 钉钉Api 帮助类
        /// </summary>
        private static IDingPlatformApiHelper _dingInstance { get; set; }
        /// <summary>
        /// 企业微信Api 帮助类
        /// </summary>
        private static ICorpWXPlatformApiHelper _corpwxInstance { get; set; }
        /// <summary>
        /// 微信小程序Api 帮助类
        /// </summary>
        private static IMiniWXPlatformApiHelper _miniwxInstance { get; set; }
        /// <summary>
        /// 短信Api 帮助类
        /// </summary>
        private static ISMSPlatformApiHelper _smsInstance { get; set; }

        #region 微信
        /// <summary>
        /// 获取微信平台接口访问类对象
        /// </summary>
        /// <returns></returns>
        public static IWXPlatformApiHelper GetWXInstance(int source)
        {
            ClientInfo clientInfo = new ClientInfoDAL().GetClientType(1, source);
            if (clientInfo == null)
            {
                throw new Exception("请先添加微信网关配置信息");
            }

            if (clientInfo.ClientType == (int)ClientTypeEnum.WX)
            {
                _wxInstance = new WXService.Service.WXService(clientInfo.AppId, clientInfo.AppSecret, clientInfo.ClientHost);
            }
            else
            {
                throw new NotImplementedException("暂不支持微信平台");
            }
            return _wxInstance;
        }
        #endregion

        #region 钉钉
        /// <summary>
        /// 获取钉钉平台接口访问类对象
        /// </summary>
        /// <returns></returns>
        /// <exception cref="Exception"></exception>
        /// <exception cref="NotImplementedException"></exception>
        public static IDingPlatformApiHelper GetDingInstance(int source)
        {
            ClientInfo clientInfo = new ClientInfoDAL().GetClientType(2, source);
            if (clientInfo == null)
            {
                throw new Exception("请先添加钉钉网关配置信息");
            }
            Log.Write($"source：{source}，clientInfo:{JsonConvert.SerializeObject(clientInfo)}");
            if (clientInfo.ClientType == (int)ClientTypeEnum.DT)
            {
                _dingInstance = new DingTalkService.Service.DingTalkService(clientInfo.AppId, clientInfo.AppSecret, clientInfo.ClientHost, Convert.ToInt64(clientInfo.AgendId));
            }
            else
            {
                throw new NotImplementedException("暂不支持");
            }
            return _dingInstance;
        }
        #endregion

        #region 企业微信
        /// <summary>
        /// 获取企业微信平台接口访问类对象
        /// </summary>
        /// <returns></returns>
        public static ICorpWXPlatformApiHelper GetCorpWXInstance(int source)
        {
            ClientInfo clientInfo = new ClientInfoDAL().GetClientType(3, source);
            if (clientInfo == null)
            {
                throw new Exception("请先添加企业微信网关配置信息");
            }

            if (clientInfo.ClientType == (int)ClientTypeEnum.CORPWX)
            {
                _corpwxInstance = new CorpWXService(clientInfo.CorpId, clientInfo.AppSecret, clientInfo.ClientHost, Convert.ToInt64(clientInfo.AgendId));
            }
            else
            {
                throw new NotImplementedException("暂不支持企业微信平台");
            }
            return _corpwxInstance;
        }
        #endregion

        #region 微信小程序
        /// <summary>
        /// 获取微信小程序接口访问类对象
        /// </summary>
        /// <returns></returns>
        public static IMiniWXPlatformApiHelper GetMiniWXInstance(int source)
        {
            ClientInfo clientInfo = new ClientInfoDAL().GetClientType((int)ClientTypeEnum.MINIWX, source);
            if (clientInfo == null)
            {
                throw new Exception("请先添加微信小程序配置信息");
            }

            if (clientInfo.ClientType == (int)ClientTypeEnum.MINIWX)
            {
                _miniwxInstance = new MiniWXService.Service.MiniWXService(clientInfo.AppId, clientInfo.AppSecret, clientInfo.CorpId);
            }
            else
            {
                throw new NotImplementedException("暂不支持微信小程序平台");
            }
            return _miniwxInstance;
        }
        #endregion

        #region 短信
        /// <summary>
        /// 获取短信接口访问类对象
        /// </summary>
        /// <returns></returns>
        public static ISMSPlatformApiHelper GetSMSInstance(int source)
        {
            ClientInfo clientInfo = new ClientInfoDAL().GetClientType((int)ClientTypeEnum.SMS, source);
            if (clientInfo == null)
            {
                throw new Exception("请先添加短信配置信息");
            }

            if (clientInfo.ClientType == (int)ClientTypeEnum.SMS)
            {
                _smsInstance = new SMSService.Service.SMSService(clientInfo.AppId, clientInfo.AppSecret, clientInfo.ClientHost);
            }
            else
            {
                throw new NotImplementedException("暂不支持短信");
            }
            return _smsInstance;
        } 
        #endregion
    }
}
